var u = navigator.userAgent,app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
var elemtVideo = '';
window.onload=function(){
	if (isAndroid) {
		//console.log("安卓");
		$('#ios-video').hide();
		$('#player').show();
		$('#player')[0].play();
		elemtVideo = $('#player');
	} else {
		$('#ios-video').show();
		$('#player').hide();
		$('#ios-video')[0].play();
		elemtVideo = $('#ios-video');
	}
	$(function() {
		o('play_btn').hide();
		window.onresize = function() {
			if($(window).width()>$(window).height()){
				mySwiper2.changeDirection('horizontal');
				$('.video_ele').width($(window).height());
				$('.video_ele').height($(window).width());
			}else{
				mySwiper2.changeDirection('vertical');
				$('.video_ele').width($(window).width());
				$('.video_ele').height($(window).height());
			}
		}
		 var mySwiper2 = new Swiper('#video_box', {
			initialSlide: 1,
			direction: 'vertical',
			loop: false,
			allowSlideNext: true,
			on: {
				slideNextTransitionEnd: function() {
					obj.current++;
					if(obj.current>0){
						obj.oneData = obj.videoData[obj.current - 1];
					}
					obj.twoData = obj.videoData[obj.current];
					if (obj.current < obj.videoData.length - 1) {
						obj.threeData = obj.videoData[obj.current + 1];
					} else {
						// 下划加锁
						obj.threeData = obj.videoData[0];
						this.allowSlideNext = false;
						//setTimeout(function() {console.log('没有更多了');},1000);
					}
					elemtVideo.prop('src', obj.twoData.video);
					elemtVideo.prop('poster', obj.twoData.poster);
					$("#fid").val(obj.twoData.fid);
					if(obj.current>0){
						$('#PrevEle').prop('src', obj.oneData.video);
						$('#PrevEle').prop('style', "background-image: url("+obj.oneData.poster+");");
						$('#PrevEle').prop('poster', obj.oneData.poster);
					}
					$('#NextEle').prop('src', obj.threeData.video);
					$('#NextEle').prop('poster', obj.threeData.poster);
					$('#NextEle').prop('style', "background-image: url("+obj.threeData.poster+");");
					setuinfo(obj.twoData);
					// 解锁上划
					if (obj.current > 0) {
						this.allowSlidePrev = true;
					}
					if (this.activeIndex == 2) {
						mySwiper2.slideTo(1, 0, false);
					}
					if (isAndroid) {
						audioAutoPlay("player");
					} else {
						audioAutoPlay("ios-video");
					}
					if(obj.current==obj.videoData.length-2 && obj.state=="0"){
						zeai.ajax({url:MHOST+'/video_detail'+zeai.ajxext+'submitok=ajax_list&fid='+fid.value},function(e){var rs=zeai.jsoneval(e);
							if(rs.flag==1 && rs.list.length>0){
								obj.videoData=obj.videoData.concat(rs.list);
							}
						});
					}
			   },
				slidePrevTransitionEnd: function() {
					obj.current--;
					if (obj.current == 0) {
						// 上划加锁
						this.allowSlidePrev = false;
					} else {
						obj.oneData = obj.videoData[obj.current - 1];
					}
					obj.twoData = obj.videoData[obj.current];
					obj.threeData = obj.videoData[obj.current + 1];
					elemtVideo.prop('src', obj.twoData.video);
					elemtVideo.prop('poster', obj.twoData.poster);
					$("#fid").val(obj.twoData.fid);
					$('#PrevEle').prop('style', "background-image: url("+obj.oneData.poster+");");
					$('#PrevEle').prop('src', obj.oneData.video);
					$('#PrevEle').prop('poster', obj.oneData.poster);
					$('#NextEle').prop('style', "background-image: url("+obj.threeData.poster+");");
					$('#NextEle').prop('poster', obj.threeData.poster);
					setuinfo(obj.oneData);
					if (obj.current < obj.videoData.length - 1) {// 下划解锁
						this.allowSlideNext = true;
					}
					if (this.activeIndex == 0) {
						mySwiper2.slideTo(1, 0, false);
					}
					if (isAndroid) {
						audioAutoPlay("player");
					} else {
						audioAutoPlay("ios-video");
					}
				},
			}
		})
	})
	agreeObj.onclick=function(){
		var em = this.children[0];
		if(em.hasClass('ed'))return;
		zeai.ajax({url:MHOST+'/video'+zeai.extname,js:1,data:{submitok:'ajax_agree',fid:o(fid).value}},function(e){var rs=zeai.jsoneval(e);
			if(rs.flag==1){
				if(!em.hasClass('ed')){
					em.addClass('ed');
					var p=agreeObj.lastChild;
					p.html(parseInt(p.innerHTML)+1);
					zeai.msg(rs.msg);
				}
			}
		});
	}
	gzObj.onclick=function(){
		zeai.ajax({url:MHOST+'/u'+zeai.extname,js:1,data:{submitok:'ajax_gz',uid:o(uid).value}},function(e){var rs=zeai.jsoneval(e);
			zeai.msg(rs.msg);
			if(rs.flag==1){
				gzObj.addClass('ed');gzObj.html('&#xe6b1; 已关注');
			}else if(rs.flag==0){
				if(gzObj.hasClass('ed'))gzObj.removeClass('ed');
				gzObj.html('&#xe622; 关注');
			}
		});
	}
}
function setuinfo(obj_){
	o('uid').value=obj_.uid;
	//o('photo_s').value=obj_.photo_s;
	//o('gzflag').value=obj_.gzflag;
	//o('agree').value=obj_.agree;
	//o('click').value=obj_.click;
	if(obj_.gzflag==1){
		if(!gzObj.hasClass('ed'))gzObj.addClass('ed');
		gzObj.html('&#xe6b1; 已关注');
	}else{
		if(gzObj.hasClass('ed'))gzObj.removeClass('ed');
		gzObj.html('&#xe622; 关注');
	}
	agreeObj.children[1].html(obj_.agree);
	clickObj.html(obj_.click);	
	photo_sObj.src=obj_.photo_s;
	uidObj.href=MHOST+'/u.php?uid='+obj_.uid;
	udataA.href=MHOST+'/u.php?uid='+obj_.uid;
	o('udata').html(decodeURIComponent(obj_.udata));
	nickname.html(html_decode(obj_.nickname));
}				
function goPlay(e) {
    var e = e || event;
    e.cancelBubble = true;
    var that = e.target || e.srcElement;
    if (that.isFirstLoadVideo) {
        that.mySwiper.unlockSwipeToNext();
        that.isFirstLoadVideo = false;
    }
    if (isAndroid) {
        var ele = o('player');
    } else {
        var ele = o('ios-video');
    }
    var paused = ele.paused;
    var ended = ele.ended;
    var readyState = ele.readyState;
    if (paused) {
        ele.play();
		play_btn.hide();
        $(that).parent().find('.posterImg').hide();
        that.playBtnStatus = true;
        if (that.readyState) {
            that.playerStatus = true;
        } else {
            if (isAndroid) {
                setTimeout(function() {that.playerStatus = true;},1500);
            } else {
                that.playerStatus = true;
            }
        }
    } else {
        ele.pause();
        that.playerStatus = false;
        that.playBtnStatus = false;
        that.readyState = true;
		play_btn.show();
    }
}
function audioAutoPlay(id){
	var audio = o(id),
		play = function(){
			audio.play();
			document.removeEventListener("touchstart",play, false);
		};
	audio.oncanplay=function(){
		//准备开始播放时执行
	}
	audio.play();
	document.addEventListener("WeixinJSBridgeReady", function () {
		play();
	}, false);
	document.addEventListener('YixinJSBridgeReady', function() {
		play();
	}, false);
	document.addEventListener("touchstart",play, false);
}